package com.liuyongyi.livepush.camerapush;

import android.graphics.Camera;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.liuyongyi.livepush.camerapush.natives.PusherNative;

/**
 * Created by liuyongyi on 2017/12/31.
 */

public class LivePusher implements SurfaceHolder.Callback {
    private SurfaceHolder surface;
    private VideoPusher videoPusher;
    private AudioPusher audioPusher;
    private PusherNative pusherNative;

    public LivePusher(SurfaceHolder surface){
        this.surface = surface;
        surface.addCallback(this);
        prepare();
    }

    private void prepare() {
        pusherNative = new PusherNative();
        //实例化音频视频推流器
        VideoParms videoParms = new VideoParms(480,320, android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
        videoPusher = new VideoPusher(surface,videoParms,pusherNative);

        AudioParms audioParms = new AudioParms();
        audioPusher = new AudioPusher(audioParms,pusherNative);
    }

    public void switchCamera(){
        videoPusher.switChangeCamera();
    }

    //开始推流
    public void startPusher(String url) {
        videoPusher.startPusher();
        audioPusher.startPusher();
        pusherNative.startPusher(url);
    }
    //停止推流
    public void stopPusher() {
        videoPusher.stopPusher();
        audioPusher.stopPusher();
        pusherNative.stopPusher();
    }

    public void release(){
        videoPusher.release();
        audioPusher.release();
        pusherNative.release();
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        stopPusher();
        release();
    }
}
